import qs from 'qs'
export default {
    name: 'Mailconfig',
    components: {

    },
    data() {
        return {
            mailset: {
                email: "",
                password: "",
                requesUrl: "",
            },
            users: "",
        }
    },
    computed: {

    },
    watch: {

    },
    created() {
        this.users = JSON.parse(window.localStorage.getItem("userInfo"));
        this.$http.get("/emailController/findEmail?userId=" + this.users.uId)
            .then((res) => {
              if(res.status == 200){
                 this.mailset.email = res.data.fromEmail;
                 this.mailset.password = res.data.fromEmailPassword;
                 this.mailset.requesUrl = res.data.emailHost;
              }
            }).catch((err) => {
            console.log(err);
        });
    },
    mounted() {

    },
    methods: {
        //邮箱配置
        mailSet() {
            var verifyObj = this.verifyForm();
            if (!verifyObj.status) {
                this.$message.error(verifyObj.message);
                return;
            };
            var postData = {
                userId: this.users.uId,
                fromEmail: this.mailset.email,
                fromEmailPassword: this.mailset.password,
                emailHost: this.mailset.requesUrl,
               
            }
            this.$http.post("/emailController/addEmail", qs.stringify(postData))
              .then((res) => {
                if(res.status == 200){
                  this.$message({
                      type:"success",
                      message:"添加成功"
                  });
                  return false;
                }
              }).catch((err) => {
               console.log(err);
           });
        },
        verifyForm() {
            var returnData = {
                status: false,
                message: ''
            }

            var emailRule = /^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/; //邮箱正则
            if (!emailRule.test(this.mailset.email)) {
                returnData.message = '邮箱格式错误';
                return returnData;
            };

            var passRule = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$/; //密码正则
            if (!passRule.test(this.mailset.password)) {
                returnData.message = '密码由英文和数字组合,6-10、2位';
                return returnData;
            }

            returnData.status = true;
            return returnData;
        },
    },
}